import { createRouter, createWebHistory  } from 'vue-router'
import { useUserStore } from '../store/user.js'
const routes = [
  {
    path: '/',
    redirect:'/home'
  },
  {
    path: '/home',
    name: 'home',
    component: ()=>import('../view/home.vue')
  },
  {
    path: '/course',
    name: 'course',
    component: ()=>import('../view/Course.vue')
  },
   {
    path: "/course-info/:id",
    name: "CourseInfo",
    component: () =>import('../view/ourse-Info.vue'),
  },
    {
    path: "/login",
    name: "login",
    component: () =>import('../view/login.vue'),
  },
  { 
    path:'/course-play/:courseId/:chapterId',
    name:'course-play',
    component: () =>import( "../view/CoursePlay.vue"),
  },
  { 
    path:'/confirmOrder',
    name:'confirmOrder',
    component: () =>import( "../view/confirmOrder.vue"),
  },
  {
    path: "/shopcart",
    name: "shopcart",
    component: () =>
      import( "../view/shopCart.vue"),
    beforeEnter:(to, from, next)=>{
      if( useUserStore().token){
        next();
      }else{
        next('/login');
      }
    }
},
]

const router = createRouter({
  history: createWebHistory (),
  routes
})

export default router
